package com.lovo.util;

import com.lovo.pojo.UserDO;

import java.sql.*;

public class DBUtil {
    private Connection con;
    private PreparedStatement statement;
    public Connection getConnection(){

        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/j189?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root","lovo");

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
    public void close(Connection con, Statement statement, ResultSet rs){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(statement != null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(con != null){
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public void close(Connection con,Statement statement){
        if(statement != null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(con != null){
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    public void close(){
        if(statement != null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(con != null){
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public void setAutoCommit(boolean autoCommit){
        try {
            con.setAutoCommit(autoCommit);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public void commit(){
        try {
            con.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public void rollback(){
        try {
            con.rollback();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    /**
     * 根据用户名和密码查询用户对象
     * @param username
     * @param pwd
     * @return
     */
    public UserDO getByUsernameAndPwd(String username, String pwd){
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_name=? and u_pwd=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1,username);
            statement.setObject(2,pwd);
            rs = statement.executeQuery();
            if(rs.next()){
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setAccount(rs.getString("u_name"));
                user.setPwd(rs.getString("u_pwd"));
                user.setBlance(rs.getDouble("u_blance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return user;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public UserDO getById(int id){
        con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1,id);
            rs = statement.executeQuery();
            if(rs.next()){
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setAccount(rs.getString("u_name"));
                user.setPwd(rs.getString("u_pwd"));
                user.setBlance(rs.getDouble("u_blance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return user;
    }

    /**
     * 根据用户名查询用户对象
     * @param username
     * @return
     */
    public UserDO getByUsername(String username){
        Connection con = getConnection();
        statement = null;
        ResultSet rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_name=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1,username);
            rs = statement.executeQuery();
            if(rs.next()){
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setAccount(rs.getString("u_name"));
                user.setPwd(rs.getString("u_pwd"));
                user.setBlance(rs.getDouble("u_blance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return user;
    }

    /**
     * 根据User对象中的数据进行修改
     * @param user
     */
    public void update(UserDO user){
        // 1. 创建连接
//        Connection con = getConnection();
//        PreparedStatement statement = null;
        String sql = "update t_user set u_pwd=?,u_blance=? where u_id=?";
        try {
            // 2. 获取执行SQL语句对象
            statement = con.prepareStatement(sql);
            statement.setObject(1,user.getPwd());
            statement.setObject(2,user.getBlance());
            statement.setObject(3,user.getId());
            // 3. 执行SQL语句
            int row = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4. 关闭连接
//            close(con,statement);
        }
    }




}
